<?php
/**
 * BootAdmin is committed to providing solutions for quickly building web applications
 * Please view the LICENSE file that was distributed with this source code,
 * For the full copyright and license information.
 * Thank you very much for using BootAdmin.
 *
 * @Author G.r<root@BootAdmin.com>
 */

declare (strict_types=1);

namespace app\admin\validate;

use app\admin\model\Admin;
use app\common\validate\BaseValidate;

/**
 * 管理员验证器
 */
class AdminValidate extends BaseValidate
{
    protected $rule = [
        'id' => 'require|checkAdmin',
        'username' => 'require|length:5,32|unique:'.Admin::class,
        'nickname' => 'require|length:1,16|unique:'.Admin::class,
        'mobile' => 'require|mobile|unique:'.Admin::class,
        'email' => 'require|email|unique:'.Admin::class,
        'password' => 'require|length:6,32|edit',
        'password_confirm' => 'requireWith:password|confirm',
        'dept_id' => 'require',
        'role_ids' => 'require',
        'status' => 'require|in:0,1|checkAbleDisable'
    ];

    protected $message = [
        'id.require' => '管理员id不能为空',
        'username.require' => '用户名不能为空',
        'username.length' => '用户名长度须在5-32位字符',
        'username.unique' => '用户名已存在',
        'password.require' => '密码不能为空',
        'password.length' => '密码长度须在6-32位字符',
        'password_confirm.requireWith' => '确认密码不能为空',
        'password_confirm.confirm' => '两次输入的密码不一致',
        'nickname.require' => '名称不能为空',
        'nickname.length' => '名称须在1-16位字符',
        'nickname.unique' => '名称已存在',
        'dept_id.require' => '请选择部门',
        'role_ids.require' => '请选择角色',
        'status.require' => '请选择状态',
        'status.in' => '状态值错误'
    ];

    /**
     * 添加场景
     * @return AdminValidate
     */
    public function sceneAdd()
    {
        return $this->remove(['password', 'edit'])
            ->remove('id', 'require|checkAdmin')
            ->remove('status', 'checkAbleDisable');
    }

    /**
     * 详情场景
     * @return AdminValidate
     */
    public function sceneDetail()
    {
        return $this->only(['id']);
    }

    /**
     * 编辑场景
     * @return AdminValidate
     */
    public function sceneEdit()
    {
        return $this->remove('password', 'require|length')
            ->append('id', 'require|checkAdmin');
    }


    /**
     * 删除场景
     * @return AdminValidate
     */
    public function sceneDelete()
    {
        return $this->only(['id']);
    }

    /**
     * 恢复场景
     * @return AdminValidate
     */
    public function sceneRecovery()
    {
        return $this->only(['id']);
    }

    /**
     * 重置密码场景
     * @return AdminValidate
     */
    public function scenePassword()
    {
        return $this->only(['id', 'password', 'password_confirm']);
    }



    /**
     * 编辑情况下，检查是否填密码
     * @param $value
     * @param $rule
     * @param $data
     * @return bool|string
     */
    public function edit($value, $rule, $data)
    {
        if (empty($data['password']) && empty($data['password_confirm'])) {
            return true;
        }
        $len = strlen($value);
        if ($len < 6 || $len > 32) {
            return '密码长度须在6-32位字符';
        }
        return true;
    }


    /**
     * 检查指定管理员是否存在
     * @param $value
     * @return bool|string
     */
    public function checkAdmin($value)
    {
        $admin = Admin::whereIn('id', $value)->withTrashed()->select();
        if ($admin->isEmpty()) {
            return '管理员不存在';
        }
        return true;
    }


    /**
     * 禁用校验
     * @param $value
     * @param $rule
     * @param $data
     * @return bool|string
     */
    public function checkAbleDisable($value, $rule, $data)
    {
        $admin = Admin::whereIn('id', $data['id'])->select();
        if ($admin->isEmpty()) {
            return '管理员不存在';
        }

        if ($value) {
            foreach ($admin as $item) {
                if (env('admin.super_admin') == $item->id) {
                    return '超级管理员不允许禁用';
                }
            }
        }
        return true;
    }
}